<?php
namespace App\Repository;
use App\Models\Base\Payment;
use App\Models\Base\Store;

/**
 * 店铺数据仓储类
 *
 * @package App\Http\Repository
 * @author WangBin
 * @mail: <849850352@qq.com>
 */
class StoreRepository extends BaseRepository
{
    /**
     * 保存数据
     *
     * @param array $data
     * @param int $id
     * @throws
     * @return int|mixed
     */
    public function save(array $data, int $id = 0)
    {
        $query = $id ? Store::query()->find($id) : new Store();
        collect($data)->each(function ($value, $field) use ($query) {
            $query->{$field} = $value;
        });
        return $query->save() ? $query->store_id : 0;
    }

    /**
     * 获取店铺列表
     *
     * @param array $filter
     * @param array|string[] $column
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getList(array $filter = [], array $column = ['*'])
    {
        $query = Store::query()->orderByDesc('created_at');
        if(!empty($filter['title'])) {
            $query->where('store_name', 'like', '%' . $filter['title'] . '%');
        }
        return $query;
    }
}
